﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Eis51.CampanyCMS.Business.NewsManager;
using Eis51.CampanyCMS.FrameWork;
using Eis51.CampanyCMS.Model.NewsManager;
using Eis51.CampanyCMS.Business;
using Eis51.CampanyCMS.Model.NewsManager.QueryO;
using Eis51.CampanyCMS.Model;

namespace Eis51.Web.Admin.NewsManager
{
    public partial class CustomerList : BasePage
    {


        public string Typecode
        {
            get
            {
                if (Request["Typecode"] == null)
                {
                    return string.Empty;
                }
                return Request["Typecode"];
            }
        }


        protected void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (!IsPostBack)
            {
                CustomerTypeBLL.BindDropListTreeType(drList, this.Typecode);
                if (string.IsNullOrEmpty(this.Typecode))
                {
                    drList.Items.Insert(0, new ListItem("请选择", "0"));
                }


                btnSearch_Click(null, null);
            }
        }


        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, System.EventArgs e)
        {
            CustomerQuery query = new CustomerQuery();
            query.PublishBeginDate = txtBeginDate.Text;
            query.PublishEndDate = txtEndDate.Text;
            query.Title = this.txtNewsTitle.Text.Trim();
            query.Licd = (Licd)AppContext.LCID;
            //query.CreateBy = AppContext.GetCurrentName();

            query.Type = int.Parse(drList.SelectedValue);
            //query.i

            IList it = CustomerBLL.GetInstance().Search(query);

            dgNews.Data = it;
            dgNews.DataBind();

        }

        protected void btnAdd_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("NewCustomer.aspx?Typecode=" + this.Typecode);
        }

        protected void dgNews_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Delete":
                    Customer obj = CustomerBLL.GetInstance().Get(new Customer.Key(int.Parse(e.Item.Cells[0].Text)));
                    CustomerBLL.GetInstance().Delete(obj);
                    this.btnSearch_Click(null, null);
                    break;
                case "Modify":
                    string url = "NewCustomer.aspx?seqno={0}&Typecode={1}";
                    url = string.Format(url, e.Item.Cells[0].Text, this.Typecode);
                    Response.Redirect(url);
                    break;
                case "F":
                    Customer n = CustomerBLL.GetInstance().Get(new Customer.Key(int.Parse(e.Item.Cells[0].Text)));


                    if (n != null)
                    {
                        n.Isfist = n.Isfist == 0 ? 1 : 0;
                        CustomerBLL.GetInstance().Update(n);
                        this.btnSearch_Click(null, null);
                    }
                    break;                
                default:
                    break;
            }
        }

        protected void dgNews_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {

            if (e.Item.ItemType == ListItemType.Header)
            {
                CheckBox cbxAll = e.Item.FindControl("cbxAll") as CheckBox;
                if (cbxAll != null)
                    cbxAll.Attributes.Add("onclick", "javascript:return SelectAll('" + dgNews.ClientID + "');");
            }
            if (e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.Item)
            {
                e.Item.Cells[3].Text = CustomerTypeBLL.GetInstance().Get(new CustomerType.Key(int.Parse(e.Item.Cells[3].Text))).Title;

                LinkButton btnDelete = e.Item.FindControl("btnDelete") as LinkButton;
                LinkButton btnModify = e.Item.FindControl("btnModify") as LinkButton;
                if (btnDelete != null)
                {


                    btnDelete.Attributes.Add("onclick", "javascript:return confirm('确定要删除该产品吗?');");
                }

            }

            
        }

        public string ChangeCn(object ob)
        {
            if (ob != null)
            {
                if (ob.ToString() == "1")
                {
                    return "是";
                }
                else
                {
                    return "否";
                }
            }

            return "";

        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            foreach (DataGridItem gvRow in dgNews.Items)
            {
                if (((CheckBox)(gvRow.FindControl("cbItem"))).Checked)
                {
                    string newid = gvRow.Cells[0].Text;
                    Customer obj = CustomerBLL.GetInstance().Get(new Customer.Key(int.Parse(newid)));
                    CustomerBLL.GetInstance().Delete(obj);

                }
            }

            btnSearch_Click(null, null);
        }







    }
}
